<?php
# Interface for the RAMS client library.
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

/**
 * @file
 * Implementation of the Rams interface class.
 */

require_once dirname(__FILE__) . '/../Raws.php';
require_once dirname(__FILE__) . '/../Util.php';
require_once dirname(__FILE__) . '/Rams/TrafficEntry.php';
require_once dirname(__FILE__) . '/Rams/TrafficFeed.php';
require_once dirname(__FILE__) . '/Rams/TrafficQuery.php';
require_once dirname(__FILE__) . '/Rams/StorageEntry.php';
require_once dirname(__FILE__) . '/Rams/StorageFeed.php';
require_once dirname(__FILE__) . '/Rams/StorageQuery.php';
require_once dirname(__FILE__) . '/Rams/FilterEntry.php';
require_once dirname(__FILE__) . '/Rams/FilterFeed.php';
require_once dirname(__FILE__) . '/Rams/FilterQuery.php';
require_once dirname(__FILE__) . '/Rams/GeoEntry.php';
require_once dirname(__FILE__) . '/Rams/GeoFeed.php';
require_once dirname(__FILE__) . '/Rams/GeoQuery.php';
require_once dirname(__FILE__) . '/Rams/TotalEntry.php';
require_once dirname(__FILE__) . '/Rams/TotalFeed.php';
require_once dirname(__FILE__) . '/Rams/TotalQuery.php';
require_once dirname(__FILE__) . '/Rams/ConcurrentEntry.php';
require_once dirname(__FILE__) . '/Rams/ConcurrentFeed.php';
require_once dirname(__FILE__) . '/Rams/ConcurrentQuery.php';
require_once dirname(__FILE__) . '/Rams/TtypeEntry.php';
require_once dirname(__FILE__) . '/Rams/TtypeFeed.php';
require_once dirname(__FILE__) . '/Rams/TtypeQuery.php';
require_once dirname(__FILE__) . '/Rams/PayedEntry.php';
require_once dirname(__FILE__) . '/Rams/PayedFeed.php';
require_once dirname(__FILE__) . '/Rams/PayedQuery.php';
require_once dirname(__FILE__) . '/Rams/UsedEntry.php';
require_once dirname(__FILE__) . '/Rams/UsedFeed.php';
require_once dirname(__FILE__) . '/Rams/UsedQuery.php';
require_once dirname(__FILE__) . '/Rams/UsersEntry.php';
require_once dirname(__FILE__) . '/Rams/UsersFeed.php';
require_once dirname(__FILE__) . '/Rams/Concurrent2Entry.php';
require_once dirname(__FILE__) . '/Rams/Concurrent2Feed.php';
require_once dirname(__FILE__) . '/Rams/Concurrent2Query.php';

/**
 * Class containing interface methods for RAMS API.
 **/
class Rams extends Raws
{
    public static $namespaces = array(
            'raws' => 'http://rambla.be/raws/ns-metadata/1.0');

    public function __construct($user = null, $pwd = null, $base_uri = null, $path = null, $client = null, $applicationId = 'MyCompany-MyApp-1.0', $ssl = False)
    {
      if (! $base_uri) {
        $base_uri = "http://rams.cdn01.rambla.be";
      }
      $this->registerPackage('Rams');
      $this->registerPackage('Rams_Extension');
      parent::__construct($user, $pwd, $base_uri, $path, $client, $applicationId, $ssl);
    }

    /**
     * Retrieve the feed for a RAMS Traffic instance.
     *
     * @param string $path The relative path to the directory.
     * @param TrafficQuery $query An object of type TrafficQuery, or null for no query-string.
     * @return Rams TrafficFeed object
     */
    public function getTrafficFeed($path = null, $query = null)
    {
      $resource = "traffic";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), null, $path);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource, $path);
      }
      return parent::getFeed($uri, 'Rams_TrafficFeed');
    }

    /**
     * Retrieve the feed for a RAMS Storage instance.
     *
     * @param string $path The relative path to the directory.
     * @param StorageQuery $query An object of type StorageQuery, or null for no query-string.
     * @return Rams StorageFeed object
     */
    public function getStorageFeed($path = null, $query = null)
    {
      $resource = "storage";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), null, $path);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource, $path);
      }
      return parent::getFeed($uri, 'Rams_StorageFeed');
    }
    
    /**
     * Retrieve the feed for a RAMS Filter instance.
     *
     * @param string $path The relative path to the directory.
     * @param FilterQuery $query An object of type FilterQuery, or null for no query-string.
     * @return Rams FilterFeed object
     */
    public function getFilterFeed($path = null, $query = null)
    {
      $resource = "filter";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), null, $path);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource, $path);
      }
      return parent::getFeed($uri, 'Rams_FilterFeed');
    }

    /**
     * Retrieve the feed for a RAMS Domain instance.
     *
     * @param string $path The relative path to the directory.
     * @return Rams GeoFeed object
     */
    public function getGeoDomainFeed($query = null)
    {
      $resource = "geo/domain";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), $resource);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource);
      }
      return parent::getFeed($uri, 'Rams_GeoFeed');
    }

    /**
     * Retrieve the feed for a RAMS City instance.
     *
     * @param string $path The relative path to the directory.
     * @return Rams GeoFeed object
     */
    public function getGeoCityFeed($query = null)
    {
      $resource = "geo/city";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), $resource);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource);
      }
      return parent::getFeed($uri, 'Rams_GeoFeed');
    }

    /**
     * Retrieve the feed for a RAMS Host instance.
     *
     * @param string $path The relative path to the directory.
     * @return Rams GeoFeed object
     */
    public function getGeoHostFeed($query = null)
    {
      $resource = "geo/host";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), $resource);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource);
      }
      return parent::getFeed($uri, 'Rams_GeoFeed');
    }

    /**
     * Retrieve the feed for a RAMS Total instance.
     *
     * @param TotalQuery $query An object of type TotalQuery, or null for no query-string.
     * @return Rams TotalFeed object
     */
    public function getTotalFeed($query = null)
    {
      $resource = "total";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), $resource);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource);
      }
      return parent::getFeed($uri, 'Rams_TotalFeed');
    }
    

    /**
     * Retrieve the feed for a RAMS Concurrent instance.
     *
     * @param ConcurrentQuery $query An object of type TrafficQuery, or null for no query-string.
     * @return Rams ConcurrentFeed object
     */
    public function getConcurrentV1Feed($query = null)
    {
      $resource = "concurrent";
      $full_path = "v1/";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), null, $full_path);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource, $full_path);
      }
      return parent::getFeed($uri, 'Rams_ConcurrentFeed');
    }
    
    /**
     * Retrieve the feed for a RAMS Concurrent instance.
     *
     * @param string $app Name of the wowza application for which concurrent data must be retrieved.
     * @param string $path Path to the stream for which concurrent data must be retrieved.
     * @param ConcurrentQuery $query An object of type TrafficQuery, or null for no query-string.
     * @return Rams ConcurrentFeed object
     */
    public function getConcurrentV2Feed($app = null, $path = null, $query = null)
    {
      $resource = "concurrent";
      $full_path = "v2/";
      if ($app) {
        $full_path .= trim($app, "/");
    		if ($path) {
    			$full_path .= "/" . trim($path, "/");
				}
      }

      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), $resource, $full_path);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource, $full_path);
      }
      return parent::getFeed($uri, 'Rams_Concurrent2Feed');
    }
    



    // /**
    //  * Retrieve the feed for a RAMS Concurrent instance.
    //  *
    //  * @param string $path The relative path below the virtual 'rambla_live_streams' directory, or null (for a listing request).
    //  * @param ConcurrentQuery $query An object of type TrafficQuery, or null for no query-string.
    //  * @return Rams ConcurrentFeed object
    //  */
    // public function getConcurrentFeed($path = null, $query = null)
    // {
    //   $resource = "concurrent";
    //   $full_path = "v1/";
    //   if ($path) {
    //      $full_path .= trim($path, "/");
    //   }
    //   if ($query) {
    //     # let the query-obj compose the uri, when present
    //     $uri = $query->getQueryUrl($this->getBaseUri(), null, $full_path);
    //   }
    //   else {
    //     # no query obj, compose URI without query-string
    //     $uri = safe_raws_uri($this->getBaseUri(), $resource, $full_path);
    //   }
    //   return parent::getFeed($uri, 'Rams_ConcurrentFeed');
    // }
    // 

    /**
     * Retrieve the feed for a RAMS traffic-type instance.
     *
     * @param TrafficTypeQuery $query An object of type TtypeQuery, or null for no query-string.
     * @return Rams TrafficTypeFeed object
     */
    public function getTrafficTypeFeed($query = null)
    {
      $resource = "traffic-type";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), $resource);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource);
      }
      return parent::getFeed($uri, 'Rams_TtypeFeed');
    }
    
    /**
     * Retrieve the feed for a RAMS Payed instance.
     *
     * @param PayedQuery $query An object of type PayedQuery, or null for no query-string.
     * @return Rams PayedFeed object
     */
    public function getPayedFeed($query = null)
    {
      $resource = "customer/payed";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), $resource);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource);
      }
      return parent::getFeed($uri, 'Rams_PayedFeed');
    }
    
    /**
     * Retrieve the feed for a RAMS Users instance.
     *
     * @return Rams UsersFeed object
     */
    public function getUsersFeed()
    {
      $resource = "customer/users";
      $uri = safe_raws_uri($this->getBaseUri(), $resource);
      return parent::getFeed($uri, 'Rams_UsersFeed');
    }
    
    /**
     * Retrieve the feed for a RAMS Used instance.
     *
     * @param string $username The name of the user account, if used credits are requested for a specific user.
     * @return Rams UsedFeed object
     */
    public function getUsedFeed($query = null, $username = null)
    {
      $resource = "customer/used";
      if ($query) {
        # let the query-obj compose the uri, when present
        $uri = $query->getQueryUrl($this->getBaseUri(), $resource, $username);
      }
      else {
        # no query obj, compose URI without query-string
        $uri = safe_raws_uri($this->getBaseUri(), $resource, $username);
      }
      return parent::getFeed($uri, 'Rams_UsedFeed');
    }
    

}
